package tr.edu.ege.seagent.rdf.reader.tutorial;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Logger;

import org.openjena.riot.Lang;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.FileUtils;

public class ExtendedReader {
	public static void main(String[] args) throws ClassNotFoundException,
			SQLException, IOException {

		printTriples("http://live.dbpedia.org/resource/Turkey", "Turkey.rdf",
				"Extended.rdf");
	}

	// prints the triples of the given resource and its external resources to a
	// file
	public static void printTriples(String resourceURIToRead,
			String fileNameFirst, String fileNameExtended)
			throws java.lang.ClassNotFoundException, java.sql.SQLException,
			IOException {
		final String DBPEDIA = "http://live.dbpedia.org";
		int numberOfObjects;
		MyLogger.setup();
		final Logger blowfish = Logger.getLogger(MyLogger.LOGGER_NAME);


		ArrayList<Object> objectList = new ArrayList<Object>();

		// does not overwrite
		Model defaultModel = ModelFactory.createDefaultModel();
		defaultModel.read(resourceURIToRead);

		// Writer object (to the specified file)
		FileWriter fstream = new FileWriter(fileNameFirst);
		BufferedWriter out1 = new BufferedWriter(fstream);
		defaultModel.write(out1);

		StmtIterator iter = defaultModel.listStatements();
		while (iter.hasNext()) {
			String objectString; // object

			Statement stmt = iter.nextStatement(); // get next statement
			RDFNode object = stmt.getObject(); // get the object
			objectString = object.toString();

			try {

				if (object instanceof Resource) {

					if (objectString.contains("http://")
							&& !objectString.contains(DBPEDIA)) {
						objectList.add(object);

						// add info about the list
						blowfish.info(object.toString()
								+ " object added to the list");
					}
				}

			} catch (Exception e) {
				blowfish.severe(objectString + "can't be added to the list due to "
						+ e.getClass() + ".");
			}
		}

		numberOfObjects = objectList.size();
		for (int i = 0; i < numberOfObjects; i++) {
			String objectString = objectList.get(i).toString();
			try {

				defaultModel.read(objectString);

			} catch (Exception e) {
				blowfish.severe(objectString
						+ "  object caused a " + e.getClass());
			}
		}
		FileWriter fstream2 = new FileWriter(fileNameExtended);
		BufferedWriter out2 = new BufferedWriter(fstream2);
		defaultModel.write(out2, "TURTLE");
	}

	// returns the RDF triples of a given URI
	@SuppressWarnings("finally")
	public static String getTriples(String string)
			throws java.lang.ClassNotFoundException, java.sql.SQLException,
			java.io.IOException {
		String list = null;
		try {
			FileManager fManager = FileManager.get();
			fManager.addLocatorURL();
			Model linkedDataModel = fManager.loadModel(string);

			StmtIterator iter = linkedDataModel.listStatements();
			while (iter.hasNext()) {
				String subjectString; // subject
				String predicateString; // predicate
				String objectString; // object

				Statement stmt = iter.nextStatement(); // get next statement
				Resource subject = stmt.getSubject(); // get the subject
				Property predicate = stmt.getPredicate(); // get the predicate
				RDFNode object = stmt.getObject(); // get the object

				subjectString = subject.toString();
				predicateString = predicate.toString();

				if (object instanceof Resource) {
					objectString = object.toString();
				} else {
					// object is a literal
					objectString = "/" + object.toString();
				}

				// add a new triple found for the subject
				list = list + subjectString + "    " + predicateString + "    "
						+ objectString + "\n";
			}

		} finally {
			return list;
		}
	}
}
